<?php
	
class Default_Model_Yatzy_Game
{
	private $die1;
	private $die2;
	private $die3;
	private $die4;
	private $die5;
	private $diceThrow = 0;
	
	private $result;
	
	public function __construct()
	{
		$this->die1 = new Default_Model_Yatzy_Dice();
		$this->die2 = new Default_Model_Yatzy_Dice();
		$this->die3 = new Default_Model_Yatzy_Dice();
		$this->die4 = new Default_Model_Yatzy_Dice();
		$this->die5 = new Default_Model_Yatzy_Dice();
		
		$this->result = new Default_Model_Yatzy_Result();
		
	}
	
	public function play($throwD1,$throwD2,$throwD3,$throwD4,$throwD5)
	{
		
		
		if ($this->diceThrow == 3)
		{
			$throwD1 = true;
			$throwD2 = true;
			$throwD3 = true;
			$throwD4 = true;
			$throwD5 = true;
		}
		
		if($throwD1)
		{
			$this->die1->roll();
		}
		
		if($throwD2)
		{
			$this->die2->roll();
		}
		
		if($throwD3)
		{
			$this->die3->roll();
		}
		
		if($throwD4)
		{
			$this->die4->roll();
		}
		
		if($throwD5)
		{
			$this->die5->roll();
		}
		
		
		
		
		
		if($this->diceThrow == 3)
		{
			$this->diceThrow = 0;
		}
		++$this->diceThrow;
	}
	
	
	public function checkOnes()
	{
		$sum = 0;
		$arr = $this->getDiceArray();
		

		
		for($i=0; $i<count($arr); ++$i)
		{
			if($arr[$i] == 1)
			{
				$sum += $arr[$i];
			}
		}
		return $sum;
	}
	
	public function checkTwos()
	{
		$sum = 0;
		$arr = $this->getDiceArray();
		

		
		for($i=0; $i<count($arr); ++$i)
		{
			if($arr[$i] == 2)
			{
				$sum += $arr[$i];
			}
		}
		return $sum;
	}
	
	public function checkThrees()
	{
		$sum = 0;
		$arr = $this->getDiceArray();
		

		
		for($i=0; $i<count($arr); ++$i)
		{
			if($arr[$i] == 3)
			{
				$sum += $arr[$i];
			}
		}
		return $sum;
	}
	
	public function checkFours()
	{
		$sum = 0;
		$arr = $this->getDiceArray();
		

		
		for($i=0; $i<count($arr); ++$i)
		{
			if($arr[$i] == 4)
			{
				$sum += $arr[$i];
			}
		}
		return $sum;
	}
	
	public function checkFives()
	{
		$sum = 0;
		$arr = $this->getDiceArray();
		

		
		for($i=0; $i<count($arr); ++$i)
		{
			if($arr[$i] == 5)
			{
				$sum += $arr[$i];
			}
		}
		return $sum;
	}
	
	public function checkSixes()
	{
		$sum = 0;
		$arr = $this->getDiceArray();
		
		for($i=0; $i<count($arr); ++$i)
		{
			if($arr[$i] == 6)
			{
				$sum += $arr[$i];
			}
		}
		return $sum;
	}
	
	public function checkPair()
	{
		$arr = $this->getDiceArray();
		sort($arr);
		rsort($arr);
		$ret = 0;
		if ($arr[0] == $arr[1]) {
			$ret = 2 * $arr[0];
		} elseif($arr[1] == $arr[2]) {
			$ret = 2 * $arr[1];
		} elseif($arr[2] == $arr[3]) {
			$ret = 2 * $arr[2];
		} elseif($arr[3] == $arr[4]) {
			$ret = 2 * $arr[3];
		}
		return $ret;
	}
	
	public function checkTwoPair()
	{
		$arr = $this->getDiceArray();
		sort($arr);
		$ret = 0;
		$pairOne;
		$pairTwo;
		if ($arr[0] == $arr[1] || $arr[0] == $arr[2] || $arr[1] == $arr[2]){
			if ($arr[0] == $arr[1]){
				$pairOne = $arr[0];
			}else{
				$pairOne = $arr[2];
			}
			if ($arr[2] == $arr[3] || $arr[2] == $arr[4] || $arr[3] == $arr[4]){
				if ($arr[2] == $arr[3]){
					$pairTwo = $arr[2];
				}else{
					$pairTwo = $arr[4];					
				}
				if ($pairOne != $pairTwo){
					$ret = 2 * ($pairOne + $pairTwo);
				}
			}
		}
		return $ret;
	}
	
	public function checkThreeOfAKind()
	{
		$arr = $this->getDiceArray();
		sort($arr);
		$ret = 0;
		if (($arr[0] == $arr[1] && $arr[0] == $arr[2]) || ($arr[1] == $arr[2] && $arr[1] == $arr[3]) || ($arr[2] == $arr[3] && $arr[2] == $arr[4])) {
			$ret = 3 * $arr[2];
		}		
		return $ret;
	}
	
	public function checkFourOfAKind()
	{
		$arr = $this->getDiceArray();
		sort($arr);
		$ret = 0;
		if (($arr[0] == $arr[1] && $arr[0] == $arr[2] && $arr[0] == $arr[3]) || ($arr[1] == $arr[2] && $arr[1] == $arr[3] && $arr[1] == $arr[4])) {
			$ret = 4 * $arr[2];
		}		
		return $ret;		
	}
	
	public function checkSmallStraight()
	{
		$sum = 0;
		$arr = $this->getDiceArray();
		sort($arr);
		
		if($arr[0] == 1 && $arr[1] == 2 && $arr[2] == 3 && $arr[3] == 4 && $arr[4] == 5)
		{
			$sum = 15;
		}
		return $sum;
	}
	
	public function checkBigStraight()
	{
		$sum = 0;
		$arr = $this->getDiceArray();
		sort($arr);
		
		if($arr[0] == 2 && $arr[1] == 3 && $arr[2] == 4 && $arr[3] == 5 && $arr[4] == 6)
		{
			$sum = 20;
		}
		return $sum;
	}
	
	public function checkFullHouse()
	{
		$arr = $this->getDiceArray();
		sort($arr);
		$ret = 0;
		if ((($arr[1] != $arr[2]) && ($arr[0] == $arr[1] && $arr[2] == $arr[3] && $arr[2] == $arr[4])) || 
			(($arr[2] != $arr[3]) && ($arr[0] == $arr[1] && $arr[1] == $arr[2] && $arr[3] == $arr[4]))) {
			$ret = $arr[0] + $arr[1] + $arr[2] + $arr[3] + $arr[4];
		}		
		return $ret;
	}
	
	public function checkChance()
	{
		$arr = $this->getDiceArray();
		return $arr[0] + $arr[1] + $arr[2] + $arr[3] + $arr[4];
	}
	
	public function checkYatzy()
	{
		$sum = 0;
		$arr = $this->getDiceArray();
		
		if($arr[0] == $arr[1] && $arr[0] == $arr[2] && $arr[0] == $arr[3] && $arr[0] == $arr[4])
		{
			$sum = 50;
		}
		return $sum;
	}
	
	private function getDiceArray()
	{
		return array($this->die1->getValue(),$this->die2->getValue(),$this->die3->getValue(),$this->die4->getValue(),$this->die5->getValue());
	}
	
	public function setDie1($die)
	{
		$this->die1 = $die1;
		return $this;
	}

	public function setDie2($die)
	{
		$this->die2 = $die2;
		return $this;
	}

	public function setDie3($die)
	{
		$this->die3 = $die3;
		return $this;
	}

	public function setDie4($die)
	{
		$this->die4 = $die4;
		return $this;
	}

	public function setDie5($die)
	{
		$this->die5 = $die5;
		return $this;
	}
	
	public function getDie1()
	{
		return $this->die1;
	}
	
	public function getDie2()
	{
		return $this->die2;
	}
	
	public function getDie3()
	{
		return $this->die3;
	}
	
	public function getDie4()
	{
		return $this->die4;
	}
	
	public function getDie5()
	{
		return $this->die5;
	}
	
	public function getDiceThrow()
	{
		return $this->diceThrow;
	}

	public function setDiceThrow($value)
	{
		$this->diceThrow = $value;
	}
	
	public function getResult()
	{
		return $this->result;
	}
	
	public function setResult($result)
	{
		$this->result = $result;
		return $this;
	}
}
	
	
	
?>